package com.example.myapplication

import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.blankj.utilcode.util.AppUtils
import com.bumptech.glide.module.ManifestParser
import com.example.mvp.view.BaseActivity
import com.example.myapplication.entity.BanBen_Entity
import com.example.yoho.mvp.entitiy.VersionEntity
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.mvp.model.LjlModel
import com.example.youhuo.mvp.presenter.BasePresenter
import com.example.youhuo.mvp.presenter.LjlPresenter
import com.example.youhuo.network.Api
import com.example.youhuo.network.HttpType
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.FileCallback
import com.lzy.okgo.model.Response
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.jar.Manifest

class Main2Activity : BaseActivity<LjlPresenter>(), LjlContract.LjlView {
    var packManager: PackageManager? = null
    private val mainBu: Button by lazy { findViewById<Button>(R.id.main_bu) }

    override fun findlayout(): Int {
        return R.layout.activity_main2

    }

    override fun initView() {
        requestPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE),100)

    }

    override fun initData() {

        LjlRetrifit()
    }

    override fun showmsg(entity: BaseEntity) {
        if (entity is BanBen_Entity) {
            var banbenEntity = entity as BanBen_Entity
            var apkData = banbenEntity.apkData
            var versionCode = apkData?.get(0)?.versionCode
            Log.e("ljl", "" + versionCode)

        }
    }

    fun getVersionCode(): Int {
        packManager = this.packageManager
        var packageInfo = packManager!!.getPackageInfo("com.example.myapplication", 0)
        var versionCode = packageInfo.versionCode
        return versionCode
    }

    fun getVersionName(): String {
        var packManager1 = this.packManager
        var packageInfo = packManager1!!.getPackageInfo("com.example.myapplication", 0)
        var versionName = packageInfo.versionName
        return versionName
    }

    fun LjlRetrifit() {

        var builder1: Retrofit.Builder = Retrofit.Builder()
        builder1.baseUrl("http://10.161.7.52/")
        builder1.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        builder1.addConverterFactory(GsonConverterFactory.create())

        var build = builder1.build()
        build.create(Api::class.java)
            .getVersion()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(object : Observer<VersionEntity> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: VersionEntity) {
                    var url = t.url
                    var versionCode = t.versionCode

                    if (versionCode.toInt()>getVersionCode()){

                        xiazai(url)
                    }else{

                    }



                }

                override fun onError(e: Throwable) {

                }

                override fun onComplete() {
                }
            })

    }
    fun xiazai(url:String){
        mainBu.setOnClickListener(View.OnClickListener {
            OkGo.get<File>(url).execute(object : FileCallback() {
                override fun onSuccess(response: Response<File>?) {

                    var body = response!!.body()

                    AppUtils.installApp(body)
                }

                override fun onError(response: Response<File>?) {

                    super.onError(response)
                }
            })
        })

    }


}
